#include "PlayState.h"
#include "Game.h"
#include "TextureManager.h"
#include "GenericButton.h"
#include "MenuButton.h"
#include "PlayState.h"
#include "Enemy.h"
#include "Defines.h"
#include "Wall.h"
#include <stdio.h>
#include <stdlib.h>
#include "InputHandler.h"



const std::string PlayState::PlayID = "PLAY";

float bulletCurTime, bulletPrevTime, bulletTimer = 1000;
float waveCurTime, wavePrevTime, waveTimer = 1000;

void PlayState::Update()
{
	int i, x, y, z;
	for(i = 0; i < mGameObjects.size(); i++)
	{
		mGameObjects[i]->Update();
		if (!mGameObjects[i]->isActive())
		{
			mGameObjects[i]->Shutdown();
			mGameObjects.erase(mGameObjects.begin() + i);
		}
	}

	if (InputHandler::Instance()->Clicked())
	{
		if (player->Purchase(resourceMap["wall"]))
		{
			Vector2D* pos = InputHandler::Instance()->getMousePosition();
			x = pos->getX();
			z = (x % 32);
			x -= z;
			y = pos->getY();
			z = (y % 32);
			y -= z;
			GameObject* newWall = new Wall(new LoadedParams(x, y, 32, 32, "wall"), 1, 1, 3, player);
			mGameObjects.push_back(newWall);
		}
	}
}

void PlayState::Draw()
{
	mMap->Draw();
	for(int i = 0; i < mGameObjects.size(); i++)
	{
		mGameObjects[i]->Draw();
	}
}

bool PlayState::OnEnter()
{
	
	if(!TextureManager::Instance()->Load(".\\assets\\Wall.png", "wall", Game::Instance()->getRenderer()))
		return false;
	if(!TextureManager::Instance()->Load(".\\assets\\StoneWall.png", "stonewall", Game::Instance()->getRenderer()))
		return false;
	if(!TextureManager::Instance()->Load(".\\assets\\Tower.png", "tower", Game::Instance()->getRenderer()))
		return false;
	if(!TextureManager::Instance()->Load(".\\assets\\Upgrade.png", "upgrade", Game::Instance()->getRenderer()))
		return false;
	if(!TextureManager::Instance()->Load(".\\assets\\Sell.png", "sell", Game::Instance()->getRenderer()))
		return false;
	if (!TextureManager::Instance()->Load(".\\assets\\GridBlock.png", "grid", Game::Instance()->getRenderer()))
		return false;
	if (!TextureManager::Instance()->Load(".\\assets\\Green.png", "green", Game::Instance()->getRenderer()))
		return false;
	if (!TextureManager::Instance()->Load(".\\assets\\HealthBg.png", "health_bg", Game::Instance()->getRenderer()))
		return false;
	if (!TextureManager::Instance()->Load(".\\assets\\HealthBar.png", "health_bar", Game::Instance()->getRenderer()))
		return false;


	player = new Player();
	player->LoadPlayerData();

	mMap = new Map();

	GameObject* wall = new Wall(new LoadedParams(320, 32*5, 32, 32, "wall"), 1, 1, 3, player);
	mGameObjects.push_back(wall);

	resourceMap["wall"] = { 30, 0, 0 };

	printf("Entering PlayState\n");
	return true;
}

bool PlayState::OnExit()
{
	for(int i = 0; i < mGameObjects.size(); ++i)
	{
		mGameObjects[i]->Shutdown();
	}

	mGameObjects.clear();
	TextureManager::Instance()->clearFromTextureMap("spaceship");
	TextureManager::Instance()->clearFromTextureMap("enemy0");
	TextureManager::Instance()->clearFromTextureMap("enemy1");
	TextureManager::Instance()->clearFromTextureMap("enemy2");
	TextureManager::Instance()->clearFromTextureMap("enemy3");
	TextureManager::Instance()->clearFromTextureMap("boss0");
	TextureManager::Instance()->clearFromTextureMap("bullet");
	printf("Exiting PlayState");
	return true;
}